<template>
  <div class="search">
    <a-card :hoverable="true" size="small">
      <div v-if="generateTaskFormVisible">
        <k-form-build ref="taskForm" :value="taskFormJson" :default-value="taskFormData" :disabled="true"/>
      </div>
      <div v-if="generateStartFormVisible&&!generateTaskFormVisible">
        <k-form-build ref="startForm" :value="startFormJson" :default-value="processInstanceFormData" :disabled="true"/>
      </div>
    </a-card>
    <a-card :hoverable="true" style="margin-top:15px" size="small">
      <f-form ref="form" :action="api.tasktodo.complete" :form-key="this.completeForm.taskId">
        <f-form-item label="审批意见" type="textarea" name="message" :value="message" :required="true"/>
        <div style="margin-top: 5px;margin-left: 120px">
          <a-button type="primary" @click="handleSubmit">同意</a-button>
          <a-button type="default" @click="doBack" style="margin-left: 7px">退回</a-button>&nbsp;
          <a-button type="default" @click="">转办</a-button>&nbsp;
          <a-button type="default" @click="">委派</a-button>&nbsp;
        </div>
      </f-form>
    </a-card>
  </div>
</template>
<script>
  import api from './js/api'

  export default {
    name: 'taskTodoForm',
    props: {},
    data() {
      return {
        refs: this.$refs,
        api: Object.assign(api, this.api),
        isAdd: true,//是否是添加模式
        loaded: false,//是否加载完成
        completeForm: this.getFormItem(),
        activeName: 'processInstanceForm',
        startFormKey: undefined,
        taskFormKey: undefined,
        startFormJson: undefined,
        taskFormJson: undefined,
        generateStartFormVisible: false,
        generateTaskFormVisible: false,
        taskFormData: undefined,
        showBusinessKey: false,
        businessKey: undefined,
        processInstanceFormData: undefined,
        startUserId: '',
        isInitiator: false,
        selectUserVisible: false,
        selectUserType: '',
        message: '',
        dialogTaskBackNodesVisible: false
      }
    },
    methods: {
      getFormItem() {
        return {
          taskId: ''
        }
      },
      //表单赋值
      setData(data) {
        if (data && data.id) {
          this.completeForm.taskId = data.id
          this.http.get(this, {
            url: this.api.tasktodo.executeTaskData,
            params: {
              taskId: data.id
            },
            success: function(me, data) {
              me.startUserId = data.startUserId
              me.showBusinessKey = data.showBusinessKey
              me.businessKey = data.businessKey
              me.startFormKey = data.startFormKey
              me.taskFormKey = data.taskFormKey
              me.isInitiator = data.isInitiator
              if (data.renderedStartForm) {
                me.startFormJson = JSON.parse(data.renderedStartForm)
                me.processInstanceFormData = JSON.parse(data.variables.processInstanceFormData)
                me.generateStartFormVisible = true
              }
              if (data.renderedTaskForm) {
                me.taskFormJson = JSON.parse(data.renderedTaskForm)
                if (me.startFormKey && me.taskFormKey && me.startFormKey == me.taskFormKey) {
                  me.taskFormData = JSON.parse(data.variables.processInstanceFormData)
                }
                me.generateTaskFormVisible = true
              }
            }
          })
        }
      },
      doComplete() {
        this.checkMessage()
        if (this.$refs.generateFormTask) {
          this.$refs['dynamicForm'].getData().then(values => {
            if (values && values != undefined) {
              let realValues = values
              if (this.startFormKey && this.taskFormKey && this.startFormKey == this.taskFormKey) {
                let processInstanceFormData = JSON.stringify(values)
                realValues = Object.assign({ processInstanceFormData }, values)
              }
              return putAction('/flowable/task/complete', {
                taskId: this.executeTaskId,
                message: this.message,
                isInitiator: this.isInitiator,
                values: realValues
              }).then(({ msg }) => {
                this.dialogExcuteTaskVisibleInChild = false
                this.$emit('executeTaskFinished')
              })
            }
          }).catch(e => {
          })
        } else {
          putAction('/flowable/task/complete', {
            taskId: this.executeTaskId,
            message: this.message
          }).then(({ msg }) => {
            Message.success(msg)
            this.dialogExcuteTaskVisibleInChild = false
            this.$emit('executeTaskFinished')
          })
        }

      },
      //提交表单
      handleSubmit(callback) {
        let params = {
          taskId: this.completeForm.taskId,
          message: this.message
        }
        if (this.$refs.taskForm) {
          params.isInitiator = this.isInitiator
          //任务表单不为空
          this.$refs['taskForm'].getData().then(values => {
            if (values && values != undefined) {
              let realValues = values
              if (this.startFormKey && this.taskFormKey && this.startFormKey == this.taskFormKey) {
                let processInstanceFormData = JSON.stringify(values)
                realValues = Object.assign({ processInstanceFormData }, values)
              }
              params.formMap = realValues
              this.$refs.form.handlerSubmit(params, callback)
            }
          })
        } else {
          this.$refs.form.handlerSubmit(params, callback,true)
        }
      }
    },
    created() {
    },
    components: {}
  }
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
